<template>
  <a-modal
    title="查看任务"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :footer="null"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-row :gutter="25">
          <a-col :md="12" :sm="24">
            <a-form-item label="任务编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                style="width: 120%"
                readOnly
                placeholder="请输入任务编码"
                v-decorator="['code', { rules: [{ required: true, message: '请输入任务编码！' }] }]"
              />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="工单编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select :open="false" style="width: 120%" placeholder="请选择工单编号" v-decorator="['workOrderId', {rules: [{ required: false, message: '请选择工单编号！' }]}]">
                <a-select-option disabled="true" v-for="(item,index) in list.workOrderList" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="工序名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select :open="false" style="width: 120%" readOnly placeholder="请选择工序名称" v-decorator="['workStepId', {rules: [{ required: false, message: '请选择工序名称！' }]}]">
                <a-select-option disabled="true" v-for="(item,index) in list.workStepList" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select></a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="报工权限"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入报工权限" readOnly v-decorator="['sysUserName', {rules: [{required: true, message: '请输入报工权限！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="产品名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select :open="false" style="width: 120%" placeholder="请选择产品名称" v-decorator="['proId', {rules: [{ required: false, message: '请选择产品名称！' }]}]">
                <a-select-option disabled="true" v-for="(item,index) in list.proList" :key="index" :value="item.id">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="计划时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-range-picker
                :disabled="true"
                :show-time="{ format: 'HH:mm' }"
                v-decorator="['startEndTime', {rules: [{required: false, message: '请选择计划时间！'}]}]"
                format="YYYY-MM-DD HH:mm"
                style="width: 120%"
                @change="startEndTimeOnChange"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="实际时间"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-range-picker
                :disabled="true"
                :show-time="{ format: 'HH:mm' }"
                v-decorator="['factTime', {rules: [{required: false, message: '请选择实际时间！'}]}]"
                format="YYYY-MM-DD HH:mm"
                style="width: 120%"
                @change="factTimeOnChange"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="计划数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input readOnly style="width: 120%" placeholder="请输入计划数" v-decorator="['plaNum', {rules: [{required: true, message: '请输入计划数！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="良品数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input readOnly style="width: 120%" placeholder="请输入良品数" v-decorator="['goodNum', {rules: [{required: true, message: '请输入良品数！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="不良品数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input readOnly style="width: 120%" placeholder="请输入不良品数" v-decorator="['badNum', {rules: [{required: true, message: '请输入不良品数！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-radio-group :disabled="true" placeholder="请选择工序状态" v-decorator="['status',{rules: [{ required: true, message: '请选择工序状态！' }]}]" >
                <a-radio :value="-1">未开始</a-radio>
                <a-radio :value="0">执行中</a-radio>
                <a-radio :value="1">已结束</a-radio>
              </a-radio-group>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { taskEdit } from '@/api/modular/main/task/taskManage'
  import { workOrderList } from '@/api/modular/main/workorder/workOrderManage'
  import { workStepList } from '@/api/modular/main/workstep/workStepManage'

  import { proList } from '@/api/modular/main/pro/proManage'
  export default {
    data () {
      return {
        list: {
          statusList: [{ 'name': '未开始', code: '-1' },
            { 'name': '执行中', code: '0' },
            { 'name': '已结束', code: '1' }],
          workOrderList: [],
          workStepList: [],
          MoIdList: [],
          proList: []
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        startEndTimeDateString: '',
        factTimeDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      detail (record) {
        this.visible = true
        workOrderList().then(res => {
          this.list.workOrderList = res.data
        })
        workStepList().then(res => {
          this.list.workStepList = res.data
        })
        proList().then(res => {
          this.list.proList = res.data
        })
        setTimeout(() => {
          if (record.plaEndTime) {
            this.plaEndTime = moment(record.plaEndTime).format('YYYY-MM-DD', 'HH:mm')
            this.plaStartTime = moment(record.plaStartTime).format('YYYY-MM-DD', 'HH:mm')
            this.form.getFieldDecorator('startEndTime', { initialValue: [moment(record.plaStartTime), moment(record.plaEndTime)] })
            this.startEndTimeDateString = [this.plaStartTime, this.plaEndTime]
          } if (record.factEndTime) {
            this.factEndTime = moment(record.factEndTime).format('YYYY-MM-DD', 'HH:mm')
            this.factStaTime = moment(record.factStaTime).format('YYYY-MM-DD', 'HH:mm')
            this.form.getFieldDecorator('factTime', { initialValue: [moment(record.factStaTime), moment(record.factEndTime)] })
            this.factTimeDateString = [this.factStaTime, this.factEndTime]
          }
          this.form.setFieldsValue(
            {
              id: record.id,
              proId: record.proId,
              code: record.code,
              badNum: record.badNum,
              goodNum: record.goodNum,
              plaNum: record.plaNum,
              proModelId: record.proModelId,
              status: record.status,
              workOrderId: record.workOrderId,
              workStepId: record.workStepId,
              sysUserName: record.sysUserName
            }
          )
        }, 100)
       },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            if (this.startEndTimeDateString) {
              values.startEndTime = this.startEndTimeDateString.join(';')
            }
            if (this.factTimeDateString) {
              values.factTime = this.factTimeDateString.join(';')
            } else {
              this.$message.error('请选择时间')
            }
            taskEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      factTimeOnChange(date, dateString) {
        this.factTimeDateString = dateString
      },
      startEndTimeOnChange(date, dateString) {
        this.startEndTimeDateString = dateString
      },
      handleCancel () {
        this.startEndTimeDateString = ''
        this.form.getFieldDecorator('startEndTime', { initialValue: null })
        this.factTimeDateString = ''
        this.form.getFieldDecorator('factTime', { initialValue: null })
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
